package com.adams.action.view;

import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.transaction.RollbackException;

import net.sf.json.JSONObject;

import org.apache.struts2.StrutsStatics;
import org.hibernate.HibernateException;
import org.hibernate.StaleStateException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;

import com.adams.core.exception.BusinessException;
import com.adams.custom.page.util.ViewConstant;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

/**
 * 
 * File Name : ViewInterceptor.java
 * 
 * @Description : 界面解析动态拦截器
 * @author 904370
 */
public class ViewInterceptor extends AbstractInterceptor
{
	Logger logger = LoggerFactory.getLogger(ViewInterceptor.class);
	/**
	 * Description : <Detail Description for package> Author : 904370
	 */
	private static final long serialVersionUID = 15887782808159725L;
	/**
	 * Description : http请求对象 Author : 904370
	 */
	private HttpServletRequest request;
	/**
	 * Description : http答复对象 Author : 904370
	 */
	private HttpServletResponse response;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.opensymphony.xwork2.interceptor.AbstractInterceptor#intercept(com
	 * .opensymphony.xwork2.ActionInvocation)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public String intercept(ActionInvocation invocation) throws Exception
	{
		// TODO Auto-generated method stub
		ActionContext actionContext = invocation.getInvocationContext();
		request = (HttpServletRequest) actionContext
				.get(StrutsStatics.HTTP_REQUEST);
		response = (HttpServletResponse) actionContext
				.get(StrutsStatics.HTTP_RESPONSE);
		response.setContentType("text/html;charset=UTF-8");
		Map result = new HashMap();
		result.put(ViewConstant.JSON_DEAL_FAILURE, Boolean.TRUE);
		try
		{
			return invocation.invoke();
		} catch (DataAccessException ex)
		{
			result.put(ViewConstant.JSON_DEAL_MSG, "数据库访问异常！");
			logger.error("数据库访问异常！", ex);
			// ex.printStackTrace();
			sendMessage(result);
		} catch (StaleStateException ex)
		{
			result.put(ViewConstant.JSON_DEAL_MSG, "操作记录已经被修改！");
			logger.error("操作记录已经被修改！", ex);
			// ex.printStackTrace();
			sendMessage(result);
		} catch (HibernateException ex)
		{
			result.put(ViewConstant.JSON_DEAL_MSG, "操作数据库异常！");
			logger.error("操作数据库异常！", ex);
			// ex.printStackTrace();
			sendMessage(result);
		} catch (NullPointerException ex)
		{
			result.put(ViewConstant.JSON_DEAL_MSG, "空指针异常！");
			logger.error("空指针异常！", ex);
			// ex.printStackTrace();
			sendMessage(result);
		} catch (SQLException ex)
		{
			result.put(ViewConstant.JSON_DEAL_MSG, "操作数据库异常！");
			logger.error("操作数据库异常！", ex);
			// ex.printStackTrace();
			sendMessage(result);
		} catch (InternalError ex)
		{
			result.put(ViewConstant.JSON_DEAL_MSG, "Java虚拟机发生了内部错误");
			logger.error("Java虚拟机发生了内部错误", ex);
			// ex.printStackTrace();
			sendMessage(result);
		} catch (RollbackException ex)
		{
			result.put(ViewConstant.JSON_DEAL_MSG, "事务回滚异常！");
			logger.error("事务回滚异常", ex);
			// ex.printStackTrace();
			sendMessage(result);

		} catch (BusinessException ex)
		{
			result.put(ViewConstant.JSON_DEAL_MSG, ex.getErrorText());
			logger.error(ex.getErrorText(), ex);
			// ex.printStackTrace();
			sendMessage(result);
		} catch (Exception ex)
		{
			result.put(ViewConstant.JSON_DEAL_MSG, ex.getMessage());
			logger.error(ex.getMessage(), ex);
			// ex.printStackTrace();
			sendMessage(result);
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	private void sendMessage(Map result) throws IOException
	{
		String value = JSONObject.fromObject(result).toString();
		response.getWriter().write(value);
	}

}
